/*
 * ZamitaciDialog.java
 * 
 * Created on 25.11.2007, 13:10:45
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.possoft.client.gui.dialog;

import info.clearthought.layout.TableLayout;

import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

import cz.possoft.messages.PropertiesManager;
import cz.possoft.messages.Text;

/**
 *
 * @author mara
 * 
 * dialog se zobrazi pokud 
 * 1) souper neakceptoval nasi vyzvu
 * 2) spadlo spojeni se souperem
 * 3) podobne akce....
 */
public class SimpleDialog extends JDialog {
    
    public static final int X1 = 400;
    public static final int Y1 = 250;
    public static final int WIDTH = 350;
    public static final int HEIGHT = 150;
    
    private String title;
    private String message;
    
    // true pokud je prave zobrazen nektery ze simpleDialogu.
    //private static boolean shown = false;        
    
    JButton okButton;
    
    // Aktualni instance simple dialogu. Simple dialog muze byt jen jeden
    private static SimpleDialog currentInstance;

    public String getMessage() {
        return message;
    }
    
//    public static boolean getShown() {
//        return shown;
//    }
//
//    public static void setShown(boolean shown) {
//        SimpleDialog.shown = shown;
//    }        

    public void setMessage(String message) {
        this.message = message;
    }
    
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
            
    double size[][] =
        {{30, 100, 90, 100, 30},
        {20, 30, 5, 20, 45}};            
    
    // Dialog se z externiho prostredi nevytvari pres konstruktor ale pres metodu getInstance
    private SimpleDialog() {}
    
    private SimpleDialog(Frame frame, String title, boolean modal) {
        super(frame, title, modal);
    }
    
    public static SimpleDialog getInstance(String title, String message) {                
        //if ((shown == false) && (currentInstance == null)) {
        JFrame frame = new JFrame(title);
        frame.setBounds(X1, Y1, WIDTH, HEIGHT);
        SimpleDialog dialog = new SimpleDialog(frame, title, true);     
        dialog.setMessage(message);
        dialog.setTitle(title);
        SimpleDialog.currentInstance = dialog;
        dialog.setBounds(X1, Y1, WIDTH, HEIGHT);                 
        dialog.initContents();           
        //shown = true;
        dialog.setVisible(true);           
        return dialog;        
//        }
//        return currentInstance;
    }
    
//    public static SimpleDialog getInstanceProt(String title, String message) {                        
//        JFrame frame = new JFrame(title);
//        frame.setBounds(X1, Y1, WIDTH, HEIGHT);
//        SimpleDialog dialog = new SimpleDialog(frame, title, true);     
//        dialog.setMessage(message);
//        dialog.setTitle(title);
//        SimpleDialog.currentInstance = dialog;
//        dialog.setBounds(X1, Y1, WIDTH, HEIGHT);                 
//        dialog.initContents();   
//        dialog.currentInstanceAsked = false;
//        //shown = true;
//        dialog.setVisible(true);           
//        return dialog;                
//    }
    
    public static SimpleDialog getCurrentInstance() {        
        return currentInstance;
    }        
    
    public void initContents() {
       Container panel = getContentPane();
       panel.setLayout(new TableLayout(size));       
       // Pridavani komponent do hlavniho panelu
       JLabel messLabel = new JLabel(message);
       messLabel.setHorizontalAlignment(JLabel.CENTER);
       panel.add(messLabel, "1,1,3,1");       
       okButton = new JButton(PropertiesManager.getInstance().getMessageForKey(Text.OK));              
       okButton.addActionListener(new OkButtonListener());              
       panel.add(okButton, "2,3");                  
       JButton pomButton = new JButton("POM");
       pomButton.setVisible(false);
       panel.add(pomButton, "4,4");   
       setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
    
    // pri dispose u dialogu nastavime currentInstance na null.
    
    public void dispose() {
        super.dispose();        
        currentInstance = null;
    }    
    
    private class OkButtonListener implements ActionListener {
        
        // nastava pri stisku tlacitka 'prijmout'
        public void actionPerformed(ActionEvent ae) {            
            SimpleDialog dialog = SimpleDialog.getCurrentInstance();
            if (dialog != null) {
                dialog.dispose();
            }     
//            shown = false;
 //           VyzivaciDialog.setPomPlayerNullFlag(false);
        }
    }        

}
